<html>
<head>
	<meta charset="UTF-8">
	<meta content="IE=edge" http-equiv="X-UA-Compatible">
	<meta content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no, width=device-width" name="viewport">
	<title>3301：[USACO2011 Feb] Cow Line</title>
	<!-- css -->
	<link href="../css/base.min.css" rel="stylesheet">
	<link href="../css/project.min.css" rel="stylesheet">
	
	<!-- favicon -->
	<!-- ... -->
</head>
<body class="page-brand">
	<header class="header header-transparent header-waterfall ui-header">
		<ul class="nav nav-list pull-left">
			<li>
				<a data-toggle="menu" href="#menu">
					<span class="icon icon-lg">menu</span>
				</a>
			</li>
		</ul>
		<a class="header-logo header-affix-hide margin-left-no margin-right-no" data-offset-top="213" data-spy="affix">[USACO2011 Feb] Cow Line</a>
		<span class="header-logo header-affix margin-left-no margin-right-no" data-offset-top="213" data-spy="affix">[USACO2011 Feb] Cow Line</span>
	</header>
	<nav aria-hidden="true" class="menu" id="menu" tabindex="-1">
		<div class="menu-scroll">
			<div class="menu-content">
				<a class="menu-logo" href="../index.html">BZOJ离线题库</a>
				<ul class="nav">
					<li>
						<a class="waves-attach" data-toggle="collapse" href="#problems">题目</a>
						<ul class="menu-collapse collapse in" id="problems">
							<li>
								<a class="waves-attach" href="../index.html">主页</a>
							</li>
							<li>
								<a class="waves-attach" href="../list.html">题目列表</a>
							</li>
						</ul>
					</li>
					<li>
						<a class="collapsed waves-attach" data-toggle="collapse" href="#about">关于</a>
						<ul class="menu-collapse collapse" id="about">
							<li>
								<a class="waves-attach" href="../about.html">关于此项目</a>
							</li>
						</ul>
					</li>
					
				</ul>
			</div>
		</div>
	</nav>
	<main class="content">
		<div class="content-header ui-content-header">
			<div class="container">
				<h1 class="content-heading">
                [USACO2011 Feb] Cow Line                </h1>
                <p>时间限制：10s&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  空间限制：128MB</p>			</div>
		</div>
		<div class="container">
			<section class="content-inner margin-top-no">
				<div class="row">
					<div class="col-lg-13 col-md-13">
						<div class="card margin-bottom-no">
							<div class="card-main">
								<div class="card-inner">
									
                                <h3>题目描述</h3><p><p><font size="3" face="Times New Roman">The N (1 &lt;= N &lt;= 20) cows conveniently numbered 1...N are playing <br />
yet another one of their crazy games with Farmer John. The cows <br />
will arrange themselves in a line and ask Farmer John what their <br />
line number is. In return, Farmer John can give them a line number <br />
and the cows must rearrange themselves into that line. <br />
A line number is assigned by numbering all the permutations of the <br />
line in lexicographic order. <br />
<br />
Consider this example: <br />
Farmer John has 5 cows and gives them the line number of 3. <br />
The permutations of the line in ascending lexicographic order: <br />
1st: 1 2 3 4 5 <br />
2nd: 1 2 3 5 4 <br />
3rd: 1 2 4 3 5 <br />
Therefore, the cows will line themselves in the cow line 1 2 4 3 5. <br />
<br />
The cows, in return, line themselves in the configuration &quot;1 2 5 3 4&quot; and <br />
ask Farmer John what their line number is. <br />
<br />
Continuing with the list: <br />
4th : 1 2 4 5 3 <br />
5th : 1 2 5 3 4 <br />
Farmer John can see the answer here is 5 <br />
<br />
Farmer John and the cows would like your help to play their game. <br />
They have K (1 &lt;= K &lt;= 10,000) queries that they need help with. <br />
Query i has two parts: C_i will be the command, which is either 'P' <br />
or 'Q'. <br />
<br />
If C_i is 'P', then the second part of the query will be one integer <br />
A_i (1 &lt;= A_i &lt;= N!), which is a line number. This is Farmer John <br />
challenging the cows to line up in the correct cow line. <br />
<br />
If C_i is 'Q', then the second part of the query will be N distinct <br />
integers B_ij (1 &lt;= B_ij &lt;= N). This will denote a cow line. These are the <br />
cows challenging Farmer John to find their line number. <br />
<br />
有N头牛，分别用1&hellip;&hellip;N表示，排成一行。 <br />
将N头牛，所有可能的排列方式，按字典顺序从小到大排列起来。 <br />
例如：有5头牛 <br />
1st: 1 2 3 4 5 <br />
2nd: 1 2 3 5 4 <br />
3rd: 1 2 4 3 5 <br />
4th : 1 2 4 5 3 <br />
5th : 1 2 5 3 4 <br />
&hellip;&hellip; <br />
现在，已知N头牛的排列方式，求这种排列方式的行号。 <br />
或者已知行号，求牛的排列方式。 <br />
所谓行号，是指在N头牛所有可能排列方式，按字典顺序从大到小排列后，某一特定排列方式所在行的编号。 <br />
如果，行号是3，则排列方式为1 2 4 3 5 <br />
如果，排列方式是 1 2 5 3 4 则行号为5 <br />
<br />
有K次问答，第i次问答的类型，由C_i来指明，C_i要么是&lsquo;P&rsquo;要么是&lsquo;Q&rsquo;。 <br />
当C_i为P时，将提供行号，让你答牛的排列方式。当C_i为Q时，将告诉你牛的排列方式，让你答行号。 <br />
<br />
</font></p></p><hr/><h3>输入格式</h3><p><p><font size="3" face="Times New Roman">* Line 1: Two space-separated integers: N and K <br />
* Lines 2..2*K+1: Line 2*i and 2*i+1 will contain a single query. <br />
Line 2*i will contain just one character: 'Q' if the cows are lining <br />
up and asking Farmer John for their line number or 'P' if Farmer <br />
John gives the cows a line number. <br />
<br />
If the line 2*i is 'Q', then line 2*i+1 will contain N space-separated <br />
integers B_ij which represent the cow line. If the line 2*i is 'P', <br />
then line 2*i+1 will contain a single integer A_i which is the line <br />
number to solve for. <br />
<br />
第1行：N和K <br />
第2至2*K+1行：Line2*i ，一个字符&lsquo;P&rsquo;或&lsquo;Q&rsquo;，指明类型。 <br />
如果Line2*i是P，则Line2*i+1，是一个整数，表示行号； <br />
如果Line2*i+1 是Q ，则Line2+i，是N个空格隔开的整数，表示牛的排列方式。</font></p>
<p></p></p><hr/><h3>输出格式</h3><p><p><font size="3" face="Times New Roman">* Lines 1..K: Line i will contain the answer to query i. <br />
<br />
If line 2*i of the input was 'Q', then this line will contain a <br />
single integer, which is the line number of the cow line in line <br />
2*i+1. <br />
<br />
If line 2*i of the input was 'P', then this line will contain N <br />
space separated integers giving the cow line of the number in line <br />
2*i+1. <br />
第1至K行：如果输入Line2*i 是P，则输出牛的排列方式；如果输入Line2*i是Q，则输出行号</font></p></p><hr/><h3>样例输入</h3><pre>5 2
P
3
Q
1 2 5 3 4

</pre><hr/><h3>样例输出</h3><pre>
1 2 4 3 5
5
</pre><hr/><h3>提示</h3><p>没有写明提示</p><hr/><h3>题目来源</h3><p>Silver</p>
								</div>
							</div>
						</div>
					</div>
				</div>
				
				
			</section>
		</div>
	</main>

	<div class="fbtn-container">
		<div class="fbtn-inner">
			<a class="fbtn fbtn-lg fbtn-brand-accent waves-attach waves-circle waves-light waves-effect" data-toggle="dropdown" aria-expanded="true"><span class="fbtn-text fbtn-text-left">Menu</span><span class="fbtn-ori icon">apps</span><span class="fbtn-sub icon">close</span></a>
			<div class="fbtn-dropup">
				<a class="fbtn fbtn-brand waves-attach waves-circle waves-light waves-effect" href="../list.html" target="_self"><span class="fbtn-text fbtn-text-left">题目列表</span><span class="icon">menu</span></a>
				<a class="fbtn fbtn-green waves-attach waves-circle waves-effect" href="../index.html" target="_self"><span class="fbtn-text fbtn-text-left">返回主页</span><span class="icon">home</span></a>
				<a class="fbtn waves-attach waves-circle waves-effect" href="http://www.lydsy.com/JudgeOnline/submitpage.php?id=3301" target="_blank"><span class="fbtn-text fbtn-text-left">提交代码</span><span class="icon">send</span></a>
				<a class="fbtn fbtn-orange waves-attach waves-circle waves-effect" href="http://www.lydsy.com/JudgeOnline/wttl/wttl.php?pid=3301" target="_blank"><span class="fbtn-text fbtn-text-left">试题讨论</span><span class="icon">chat</span></a>
				
			</div>
		</div>
	</div>

	<!-- js -->
	<script src="../js/jquery.min.js"></script>
	<script src="../js/base.min.js"></script>
	<script src="../js/project.min.js"></script>
</body>
</html>